<template>
	<view>

		<!-- address -->
		<view class="address" v-if="choiseAddress.length==0">
		<button @click="addAddress()"> 请添加地址 </button>
			
		</view>

		<view class="address" v-else>
			<text>收货地址：</text>
			<text> {{choiseAddress.name }}</text>
			:
			<text> {{choiseAddress.address}}</text>
			<button class="addressBtn" @click="addAddress()">更换地址</button>
		</view>

		<view class="cart-title">
			<uni-icons type="shop" size='18'> </uni-icons>
			<text class="cart-title-text">购物车</text>
		</view>
		
		<view class="">
			<uni-swipe-action>
				<view class="goods_item" v-for="(item,index) in cart" :key="index" @click="bindIndex(index)">

					<uni-swipe-action-item :right-options="options1" @click="swipe_action_btn(index)">
						<text class="cart_list_item_text">
							<!-- {{item}} -->
							{{item.goods_name}}-
							price:{{item.price}}
							count:{{item.goods_count}}
						</text>
						<uni-number-box :min='1' v-model='item.goods_count' @change="numberBoxChange"></uni-number-box>
					</uni-swipe-action-item>
				</view>
			</uni-swipe-action>
		</view>
		<button @click="showStorage()">showStorage</button>
		<button @click="clearCart()">清空购物车</button>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapMutations,
		mapActions
	} from 'vuex'
	import {
		onLoad
	} from '../../uni_modules/uni-load-more/components/uni-load-more/uni-load-more.vue'
	import badgeMix from "../../static/mixin/tabbar-badge.js"

	export default {
		mixins: [badgeMix],
		computed: {
			...mapState(['goods_count', 'cart']),
		},
		data() {
			return {
				addresslist:[
					{
						name:'molu',
						address:'湖北省武汉市123'
					},
					{
						name:'hzf',
						address:'湖北省鄂州'
					},
					{
						name:'IKUN',
						address:'北京朝阳吴亦凡隔壁'
					},
					
				],		
				choiseAddress:[],			
				options1: [{
					text: '删除'
				}],
				cartList: this.cart || '[]',
				value: 1,
				//当前改变numberbox的  index
				activeIndex: 1
			}
		},
		methods: {
			...mapMutations(['addToCart', 'changeNumberValue', 'deleteOne']),
			async addAddress(){
				uni.redirectTo({
					url:'../../subpkg/address/address'
				})
			},
			swipe_action_btn(index) {
				this.deleteOne(index)
			},

			bindIndex(index) {
				this.activeIndex = index
			},
			numberBoxChange(value) {
				//写setTimeout 是因为 numberBoxChange在 bindIndex前执行
				setTimeout(() => {
					let obj = {
						value: value,
						activeIndex: this.activeIndex
					}
					this.changeNumberValue(obj)
				}, 0)



			},
			deleteitem(index) {
				// console.log(index);
				this.deleteOne(index)
			},
			clearCart() {
				uni.removeStorageSync('cart')
			},
			showStorage() {
				let res = uni.getStorageSync('cart')
				console.log(res);
			}
		},
		onShow() {
			console.log('show');
			// this.setBadge()
		},
		onLoad(){
			this.choiseAddress = uni.getStorageSync('address')
			console.log(this.choiseAddress);
		}

	}
</script>

<style lang="scss">
	.goods_item {
		padding: 10rpx;
		// display: flex;
		// flex-direction: row;
	}

	.cart_list_item_text {
		// padding:10rpx;
		max-width: 500rpx;
		white-space: pre-wrap;
		overflow: hidden;
		text-overflow: ellipsis;

	}

	.address {
		// height: 100rpx;
		background-color: lightblue;
	}
	.addressBtn{
		width: 200rpx;
		height: 90rpx;
		background-color: lightblue;
		
	}
</style>